package fr.fermeManager.application.views.components;

import java.awt.Color;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;

import net.miginfocom.swing.MigLayout;

import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;

import fr.fermeManager.library.model.beans.Transaction;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.Bindings;

public class ComponentGridTransactions extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private List<Transaction> listeTransactions;
	
	private Transaction transacSelect = new Transaction();
	
	private JTable table;
	
	/**
	 * Create the panel.
	 */
	public ComponentGridTransactions() {
		setAutoscrolls(true);
		setLayout(new MigLayout("", "[645px,grow]", "[grow]"));
		
		table = new JTable();
		table.setAutoCreateRowSorter(true);
		table.setBorder(new LineBorder(new Color(0, 0, 0)));
		table.setShowVerticalLines(false);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		//add(table, "cell 0 0,grow");
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, "cell 0 0,grow");
		
		scrollPane.setViewportView(table);
		
//		table.setPreferredScrollableViewportSize(table.getPreferredSize());
		
//		Dimension size = table.getPreferredScrollableViewportSize();
//		table.setPreferredScrollableViewportSize(new Dimension(Math.min(getPreferredSize().width, size.width), size.height));
		    
		
		initDataBindings();
	}
	
	/**
	 * @param listeTransactions the listeTransactions to set
	 */
	public void setListeTransactions(List<Transaction> listeTransactionsParam) {
		listeTransactions = listeTransactionsParam;
		initDataBindings();
	}
	
	public void setListeTransactions(List<Transaction> listeTransactionsParam,Transaction selectedTransac) {
		this.listeTransactions = listeTransactionsParam;
		initDataBindings();
		
		int indiceElem = listeTransactions.indexOf(selectedTransac);
		
		int indiceTable = table.convertRowIndexToView(indiceElem);
		
		table.getSelectionModel(). setSelectionInterval(indiceTable, indiceTable);
		
	}
	
	/**
	 * @return the transacSelect
	 */
	public Transaction getTransacSelect() {
		if(this.transacSelect.getLibelle()==null || (this.transacSelect.getLibelle().length()<1)){
			return null;
		}
		
		for (Transaction transacTmp : this.listeTransactions) {
			if(transacTmp.getId() == this.transacSelect.getId()){
				return transacTmp;
			}
		}
		
		return null;
	}

	protected void initDataBindings() {
		JTableBinding<Transaction, List<Transaction>, JTable> jTableBinding = SwingBindings.createJTableBinding(UpdateStrategy.READ_ONCE, listeTransactions, table);
		//
		BeanProperty<Transaction, Boolean> transactionBeanProperty = BeanProperty.create("actif");
		jTableBinding.addColumnBinding(transactionBeanProperty).setColumnName("Actif").setEditable(false);
		//
		BeanProperty<Transaction, Integer> transactionBeanProperty_1 = BeanProperty.create("id");
		jTableBinding.addColumnBinding(transactionBeanProperty_1).setColumnName("Id").setEditable(false);
		//
		BeanProperty<Transaction, String> transactionBeanProperty_2 = BeanProperty.create("libelle");
		jTableBinding.addColumnBinding(transactionBeanProperty_2).setColumnName("Libelle").setEditable(false);
		//
		BeanProperty<Transaction, String> transactionBeanProperty_3 = BeanProperty.create("descritption");
		jTableBinding.addColumnBinding(transactionBeanProperty_3).setColumnName("Description").setEditable(false);
		//
		BeanProperty<Transaction, Float> transactionBeanProperty_17 = BeanProperty.create("montant");
		jTableBinding.addColumnBinding(transactionBeanProperty_17).setColumnName("Montant").setEditable(false);
		//
		BeanProperty<Transaction, String> transactionBeanProperty_18 = BeanProperty.create("typeTransaction.libelle");
		jTableBinding.addColumnBinding(transactionBeanProperty_18).setColumnName("Type").setEditable(false);
		//
		jTableBinding.bind();
		//
		BeanProperty<JTable, Integer> jTableBeanProperty = BeanProperty.create("selectedElement.id");
		BeanProperty<Transaction, Integer> transactionBeanProperty_4 = BeanProperty.create("id");
		AutoBinding<JTable, Integer, Transaction, Integer> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ, table, jTableBeanProperty, transacSelect, transactionBeanProperty_4);
		autoBinding.bind();
		//
		BeanProperty<JTable, String> jTableBeanProperty_1 = BeanProperty.create("selectedElement.libelle");
		BeanProperty<Transaction, String> transactionBeanProperty_5 = BeanProperty.create("libelle");
		AutoBinding<JTable, String, Transaction, String> autoBinding_1 = Bindings.createAutoBinding(UpdateStrategy.READ, table, jTableBeanProperty_1, transacSelect, transactionBeanProperty_5);
		autoBinding_1.bind();
	}
}
